package com.itheima.HomeWork_day08;
/*
迭代器的使用
增强for的使用
以及普通for使用
 */
import java.util.ArrayList;
import java.util.Iterator;


public class IteratorTest02 {

    public static void main(String[] args) {
        //定义一个集合
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");

  //      System.out.println(list);



       IteratorList(list);

        System.out.println();
        System.out.println("-----------------------------");

     forEachList(list);

        System.out.println();
        System.out.println("-----------------------------");

           forList(list);


    }

    //普通for遍历集合
    private static void forList(ArrayList<String> list) {
        for (int i = 0; i < list.size(); i++) {
            String st=list.get(i);
            System.out.print(st+" ");
        }
    }

    //增强for遍历集合
    private static void forEachList(ArrayList<String> list) {
        for (String s : list) {
            System.out.print(s+" ");
        }
    }

    //获得迭代器的对象
    //迭代器一创建,默认指向集合0索引处
    private static void IteratorList(ArrayList<String> list) {
        Iterator<String> it = list.iterator();
        //判断返回的hasNext()

            while(it.hasNext()){
                String s = it.next();
                System.out.print(s+" ");
            }
    }
}
